from email import header


# -*- coding: utf-8 -*-
# @description: 
# @File  : app.py
# @Time  :2022-04-06 14:02:14
# @Author:selfeasy
# @Email :selfeasy@qq.com

from flask import Flask
from redis import Redis, RedisError
import os
import socket
import logging
# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
os.makedirs('logs')
logging.debug('test docker')
app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=9981)